package org.scau.studentevaluation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sun.org.apache.bcel.internal.classfile.Code;
import lombok.extern.slf4j.Slf4j;
import org.scau.studentevaluation.entity.bo.OtherActBO;
import org.scau.studentevaluation.entity.dos.OtherActDO;
import org.scau.studentevaluation.entity.enums.IsAppealedEnum;
import org.scau.studentevaluation.entity.enums.IsCheckedEnum;
import org.scau.studentevaluation.entity.enums.IsPassedEnum;
import org.scau.studentevaluation.entity.query.GetOtherActQuery;
import org.scau.studentevaluation.entity.query.UpdateOtherActQuery;
import org.scau.studentevaluation.entity.query.UploadOtherActQuery;
import org.scau.studentevaluation.entity.vo.OtherActVO;
import org.scau.studentevaluation.service.IOtherActService;
import org.scau.studentevaluation.service.ImportMapperService;
import org.scau.studentevaluation.util.BeanConvertUtils;
import org.scau.studentevaluation.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @Author: kongyuer
 * @Date: 2020/09/02 17:06
 */

@Slf4j
@Service
public class OtherActServiceImpl implements IOtherActService {

    @Autowired
    private ImportMapperService importMapperService;

    @Override
    public List<OtherActVO> getOtherActsByCollege(String college) {
        LambdaQueryWrapper<OtherActDO> query = Wrappers.<OtherActDO>lambdaQuery()
                .eq(OtherActDO::getCollege, college);
        List<OtherActDO> DOs = importMapperService.otherActMapper.selectList(query);

        List<OtherActBO> BOs = BeanConvertUtils.DOs2BOs(DOs, OtherActBO.class);
        List<OtherActVO> VOs = BeanConvertUtils.BOs2VOs(BOs,OtherActVO.class);

        return VOs;
    }

    @Override
    public List<OtherActVO> getOtherActs(GetOtherActQuery getOtherActQuery) {
        LambdaQueryWrapper<OtherActDO> query = Wrappers.<OtherActDO>lambdaQuery()
                .eq(OtherActDO::getStudentId, getOtherActQuery.getStudentId())
                .eq(OtherActDO::getOtherActYear, getOtherActQuery.getOtherActYear())
                .eq(OtherActDO::getIsPassed, getOtherActQuery.getIsPassed());
        List<OtherActDO> DOs = importMapperService.otherActMapper.selectList(query);

        List<OtherActBO> BOs = BeanConvertUtils.DOs2BOs(DOs, OtherActBO.class);
        List<OtherActVO> VOs = BeanConvertUtils.BOs2VOs(BOs,OtherActVO.class);

        return VOs;
    }

    @Override
    public int updateOtherActs(UpdateOtherActQuery updateOtherActQuery) {
        LambdaQueryWrapper<OtherActDO> query = Wrappers.<OtherActDO>lambdaQuery()
                .eq(OtherActDO::getId, updateOtherActQuery.getId());
        OtherActDO DO = importMapperService.otherActMapper.selectOne(query);
        if (DO != null) {
            DO.setIsChecked(updateOtherActQuery.getIsChecked());
            DO.setIsPassed(updateOtherActQuery.getIsPassed());
        }
        return importMapperService.otherActMapper.updateById(DO);
    }

    @Override
    public List<Long> getOtherActId (Long studentId) {
        LambdaQueryWrapper<OtherActDO> query = Wrappers.<OtherActDO>lambdaQuery()
                .eq(OtherActDO::getStudentId, studentId);
        List<OtherActDO> DOs = importMapperService.otherActMapper.selectList(query);
        List<Long> otherActIds = new ArrayList<Long>();
        for (OtherActDO otherActDO : DOs){
            otherActIds.add(otherActDO.getId());
        }
        return otherActIds;
    }

    @Override
    public int uploadOtherAct(UploadOtherActQuery uploadOtherActQuery) {
        LambdaQueryWrapper<OtherActDO> query = Wrappers.<OtherActDO>lambdaQuery()
                .eq(OtherActDO::getStudentId, uploadOtherActQuery.getStudentId())
                .eq(OtherActDO::getOtherActName, uploadOtherActQuery.getOtherActName());
        OtherActDO DOs = importMapperService.otherActMapper.selectOne(query);
        if (DOs != null) {
            return 0;
        }
        String uuidFileName= UUID.randomUUID().toString().replace("-","")+".jpg";
        String newCompanyImagepath = "static/" + uuidFileName;
        if (!uploadOtherActQuery.getActivityImage().isEmpty()) {
            try {
                //图片命名
                File newFile = new File(newCompanyImagepath);
                File parenFile = newFile.getParentFile();
                parenFile.mkdirs();
                System.out.println(newFile.getAbsoluteFile());
                if (!newFile.exists()) {
                    newFile.createNewFile();
                }
                BufferedOutputStream out = new BufferedOutputStream(
                        new FileOutputStream(newFile));
                out.write(uploadOtherActQuery.getActivityImage().getBytes());
                out.flush();
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return 0;
            } catch (IOException e) {
                e.printStackTrace();
                return 0;
            }
        }
        DOs = new OtherActDO();
        DOs.setIsPassed(IsPassedEnum.IS_PASSED_FALSE.getType());
        DOs.setAdditionalBasis(uploadOtherActQuery.getAdditionalBasis());
        DOs.setAppealRemark("");
        DOs.setCollege(uploadOtherActQuery.getCollege());
        DOs.setIsAppealed(IsAppealedEnum.IS_APPEALED_FALSE.getType());
        DOs.setIsChecked(IsCheckedEnum.IS_CHECKED_FALSE.getType());
        DOs.setOtherActImageUrl("static/" + uuidFileName);
        DOs.setOtherActName(uploadOtherActQuery.getOtherActName());
        DOs.setOtherActYear(DateUtil.YEAR());
        DOs.setStudentId(uploadOtherActQuery.getStudentId());
        return importMapperService.otherActMapper.insert(DOs);
    }
}
